package com.jzw.zhiweijin.lab;

import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XRefreshViewFooter;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    //View
    /** 刷新控件 */
    private XRefreshView xrefreshview;
    private RecyclerView recyclerViewTestRv;
    FloatingActionButton fab_top;
    //Data
    /** xRefresh的RecyclerView的Adapter */
    private SimpleAdapter simpleAdapter;
    LinearLayoutManager ll;
    RecyclerView.LayoutManager manager;
    ArrayList<String> arrayList=new ArrayList<>();

    private void assignViews() {
        xrefreshview = (XRefreshView) findViewById(R.id.xrefreshview);
        recyclerViewTestRv = (RecyclerView) findViewById(R.id.recycler_view_test_rv);
        fab_top= (FloatingActionButton) findViewById(R.id.fab_top);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        assignViews();
//        manager= RecyclerView.LayoutManager.

        ll=new LinearLayoutManager(this);
        recyclerViewTestRv.setLayoutManager(ll);
//        recyclerViewTestRv.addItemDecoration(new Divider);
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");
        arrayList.add("dddd");

        simpleAdapter=new SimpleAdapter(this,arrayList);
        recyclerViewTestRv.setAdapter(simpleAdapter);
        simpleAdapter.setCustomLoadMoreView(new RecycleViewfoot(this));
        xrefreshview.setPinnedTime(1000);
        xrefreshview.setPullLoadEnable(true);
        ListView l=new ListView(this);
        fab_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ll.scrollToPosition(0);
            }
        });
//l.setOnScrollListener(new AbsListView.OnScrollListener() {
//    @Override
//    public void onScrollStateChanged(AbsListView view, int scrollState) {
//
//    }
//
//    @Override
//    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//
//    }
//});



        recyclerViewTestRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (ll.findFirstVisibleItemPosition()<10){
                    fab_top.setVisibility(View.GONE);
                }else {
                    fab_top.setVisibility(View.VISIBLE);




                }
                Log.d("OnScrolled方法","dx="+dx+"dy="+dy+"这是"+ll.findFirstVisibleItemPosition()+"个");
            }
        });






        xrefreshview.setXRefreshViewListener(new XRefreshView.XRefreshViewListener(){

            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        xrefreshview.stopRefresh();
                    }
                },2000);
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        xrefreshview.stopLoadMore();
                    }
                },2000);
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double offset, int offsetY) {

            }
        });



    }
class MyLyaout extends RecyclerView.LayoutManager{

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return null;
    }


    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        super.onLayoutChildren(recycler, state);
    }
}

}
